package com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.view;

import com.xiaobawang.xjsyy_idea_plugin.forest.RunningHistory;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import com.xiaobawang.xjsyy_idea_plugin.service.Service;
import com.xiaobawang.xjsyy_idea_plugin.util.DateUtil;
import com.xiaobawang.xjsyy_idea_plugin.util.PagingTable;

import javax.swing.*;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.util.List;
import java.util.Vector;

/**
 * @author synhard
 * @description TODO
 * @date 2024/5/16 15:13
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class RunningHistoryPanel extends JPanel {

    private final PagingTable pagingTable;
    private final DataFetcher dataFetcher;

    public RunningHistoryPanel(Long pipelineId) {
        this.dataFetcher = (pageNumber, pageSize) -> {
            // 从数据服务中获取对应页的数据
            List<RunningHistory> runningHistoryList = Service.runningHistory(pipelineId, pageNumber, pageSize);

            Vector<Vector<Object>> tempData = new Vector<>();

            for (RunningHistory runningHistory : runningHistoryList) {
                Vector<Object> vector = new Vector<>();
                // 运行记录
                vector.add(new JLabel("#" + runningHistory.getPipelineRunId()));
                // 运行状态
                vector.add(new JLabel(runningHistory.getStatus()));
                // 代码源
                JLabel codeRepoLabel = new JLabel();
                codeRepoLabel.setLayout(new GridLayout(1, runningHistory.getCodeURLList().size()));
                for (RunningHistory.CodeSourceInfo codeSourceInfo : runningHistory.getCodeURLList()) {
                    codeRepoLabel.add(new JLabel(codeSourceInfo.getBranch()));
                }
                vector.add(codeRepoLabel);
                // 运行详情
                JLabel runningDetailLabel = new JLabel();
                runningDetailLabel.setLayout(new GridLayout(2, runningHistory.getStageInfoList().size()));
                for (RunningHistory.StageInfo stageInfo : runningHistory.getStageInfoList()) {
                    JLabel label = new JLabel(stageInfo.getName());
                    runningDetailLabel.add(label);
                }
                for (RunningHistory.StageInfo stageInfo : runningHistory.getStageInfoList()) {
                    JLabel label = new JLabel(stageInfo.getStatus());
                    runningDetailLabel.add(label);
                }
                vector.add(runningDetailLabel);
                // 触发信息
                vector.add(new JLabel(runningHistory.getCreator().getUsername() +
                        GlobalConfig.triggerModeMap.get(runningHistory.getTriggerMode())));
                // 开始时间
                vector.add(new JLabel(DateUtil.parseTimestampToDateStr(runningHistory.getStartTime())));
                // 持续时间
                vector.add(new JLabel(DateUtil.formatTimeDifference(runningHistory.getEndTime(), runningHistory.getStartTime())));
                tempData.add(vector);
            }
            return tempData;
        };
        // 示例数据
        Vector<String>         columnNames = new Vector<>();
        columnNames.add("运行记录");
        columnNames.add("状态");
        columnNames.add("源信息");
        columnNames.add("运行详情");
        columnNames.add("触发信息");
        columnNames.add("开始时间");
        columnNames.add("持续时间");
        pagingTable = new PagingTable(columnNames, dataFetcher);
        for (int i = 0; i < pagingTable.getTable().getColumnModel().getColumnCount(); i++) {
            pagingTable.getTable().getColumnModel().getColumn(i).setCellRenderer(new JLabelTableCellRenderer());
        }
        pagingTable.getMainPanel().setPreferredSize(new Dimension(1600, 600));
        TableColumnModel columnModel = pagingTable.getTable().getColumnModel();
        for (int column = 0; column < pagingTable.getTable().getColumnCount(); column++) {
            if (column == 0 || column == 2) {
                columnModel.getColumn(column).setPreferredWidth(100);
            } else if (column == 4 || column == 5 || column == 6 || column == 7) {
                columnModel.getColumn(column).setPreferredWidth(200);
            } else if (column == 3) {
                columnModel.getColumn(column).setPreferredWidth(600);
            }
        }
        pagingTable.getTable().setRowHeight(40);
        add(pagingTable.getMainPanel());
    }

    @Override
    public void removeAll() {
        pagingTable.getMainPanel().removeAll();
        super.removeAll();
    }
}
